package com.krain.meijusky.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;

import com.krain.corelibrary.util.BaseActivityManager;
import com.krain.meijusky.R;
import com.krain.meijusky.base.APP;
import com.krain.meijusky.util.BaseUtil;
import com.umeng.analytics.MobclickAgent;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * Class description
 *
 * @author yamin
 * @date 2015/11/2 下午 05:50
 */
@EActivity
public abstract class BaseActivity extends AppCompatActivity {
    @ViewById(R.id.toolbar)
    Toolbar mToolBar;
    @org.androidannotations.annotations.App
    APP app;
    @Bean
    BaseUtil baseUtil;

    BaseActivity mContext;

    boolean isExit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseActivityManager.getInstance().addActivity(this);
        mContext = this;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @AfterViews
    public void baseAfterViews() {
        initToolBar();
    }

    protected void initToolBar() {
        if (mToolBar == null) return;
        String title = getToolbarTitle();
        mToolBar.setTitle(title == null ? "" : title);
        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        mToolBar.setNavigationIcon(R.mipmap.ic_arrow_back);
        setToolbarBackClick(mToolBar);
    }

    private void setToolbarBackClick(Toolbar mToolBar) {
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                supportFinishAfterTransition();
            }
        });


    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
    }


    public void startAnimActivity(Intent intent, View shareView, String shareElement) {
        ActivityOptionsCompat optionsCompat = ActivityOptionsCompat
                .makeSceneTransitionAnimation(this, shareView, shareElement);
        optionsCompat.makeCustomAnimation(this, android.R.transition.fade, android.R.transition.fade);
        ActivityCompat.startActivity(this, intent, optionsCompat.toBundle());
    }

    /**
     * toolbar设置项
     */
    protected abstract String getToolbarTitle();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseActivityManager.getInstance().removeActivity(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode != KeyEvent.KEYCODE_BACK) return super.onKeyDown(keyCode, event);
        if (BaseActivityManager.getInstance().getActivityStackSize() == 1) {
            if (!isExit) {
                isExit = true;
                baseUtil.showSnackBar(this, "再按一次退出程序");
                revertIsExit();
            } else {
                if (baseUtil.getSnackbar() != null)
                    baseUtil.getSnackbar().dismiss();
                finish();
            }
            return true;
        } else return super.onKeyDown(keyCode, event);

    }

    @UiThread(delay = 3000)
    public void revertIsExit() {
        isExit = false;
    }
}
